package com.fxb.apollo.rest;

import static java.lang.System.out;

import java.util.Set;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;

/**
 * @author fangjiaxiaobai
 * @date 2020-07-09 16:00
 * @since 1.0.0
 */
@RestController
public class ShowConfigRest {

    @GetMapping("/key/{key}")
    public String show(@PathVariable("key") String key) {

        Config appConfig = ConfigService.getAppConfig();
        return appConfig.getProperty(key, "null");

    }

    /**
     * 监听配置更新的时候触发。推。
     *
     * tips; apollo还有一个定时拉的方式获取新配置。
     * @param configEvent 配置发生的事件，指改变的事件
     */
    @ApolloConfigChangeListener("application")
    public void listenApplicationChange(ConfigChangeEvent configEvent) {
        Set<String> strings = configEvent.changedKeys();
        out.println("\n 监听到application配置....！！");
        strings.forEach(item -> out.println(item + "->" + configEvent.getChange(item)));
    }


}
